package com.example.wei.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.wei.mvp.R;
import com.example.wei.mvp.base.BaseActivity;
import com.example.wei.mvp.component.AppComponent;
import com.example.wei.mvp.component.DaggerMainComponent;
import com.example.wei.mvp.ui.adapter.NewYDAdapter;
import com.githang.statusbar.StatusBarCompat;
import com.rd.Orientation;
import com.rd.PageIndicatorView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by Administrator on 2017/11/1 0001.
 */
public class GuidanceActivity extends BaseActivity {


    private LinearLayout.LayoutParams mParams;
    private List<View> views;
    @BindView(R.id.newyd_viewPager)
    ViewPager newydViewPager;
    @BindView(R.id.newyd_pageindicatorview)
    PageIndicatorView newydPageindicatorview;
    @BindView(R.id.newyd_tv_text)
    RelativeLayout newydTvText;

    @Override
    public int getLayoutId() {
        return R.layout.acitvity_newyindao;
    }

    @Override
    protected void attachView() {

    }

    @Override
    protected void innitView() {
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#00000000"), true);
    }

    @Override
    protected void innitData() {

        // 为引导图片提供布局参数
        mParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        views = new ArrayList<View>();
        // 初始化引导图片列表
        NewYDAdapter newYDAdapter = new NewYDAdapter();
        newYDAdapter.setData(createPageList());

        newydViewPager.setAdapter(newYDAdapter);
        newydPageindicatorview.setViewPager(newydViewPager);
        newydPageindicatorview.setOrientation(Orientation.HORIZONTAL);

        newydTvText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(GuidanceActivity.this, MainActivity.class);
                GuidanceActivity.this.startActivity(intent);
                finish();
            }
        });

        newydViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position ==2) {
                    newydTvText.setVisibility(View.VISIBLE);
                }else {
                    newydTvText.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder().appComponent(appComponent).build().inject(this);
    }


    @NonNull
    private List<View> createPageList() {
        List<View> pageList = new ArrayList<>();
        pageList.add(createPageView(R.mipmap.guidepage4a));
        pageList.add(createPageView(R.mipmap.guidepage4b));
        pageList.add(createPageView(R.mipmap.guidepage4c));
        return pageList;
    }


    @NonNull
    private View createPageView(int color) {
        View view = new View(this);
        view.setBackgroundResource(color);
        return view;
    }



    public static void scaleImage(final Activity activity, final View view, int drawableResId) {

        // 获取屏幕的高宽
        Point outSize = new Point();
        activity.getWindow().getWindowManager().getDefaultDisplay().getSize(outSize);

        // 解析将要被处理的图片
        Bitmap resourceBitmap = BitmapFactory.decodeResource(activity.getResources(), drawableResId);

        if (resourceBitmap == null) {
            return;
        }

        // 开始对图片进行拉伸或者缩放

        // 使用图片的缩放比例计算将要放大的图片的高度
        int bitmapScaledHeight = Math.round(resourceBitmap.getHeight() * outSize.x * 1.0f / resourceBitmap.getWidth());

        // 以屏幕的宽度为基准，如果图片的宽度比屏幕宽，则等比缩小，如果窄，则放大
        final Bitmap scaledBitmap = Bitmap.createScaledBitmap(resourceBitmap, outSize.x, bitmapScaledHeight, false);

        view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                //这里防止图像的重复创建，避免申请不必要的内存空间
                if (scaledBitmap.isRecycled())
                    //必须返回true
                    return true;


                // 当UI绘制完毕，我们对图片进行处理
                int viewHeight = view.getMeasuredHeight();


                // 计算将要裁剪的图片的顶部以及底部的偏移量
                int offset = (scaledBitmap.getHeight() - viewHeight) / 2;


                // 对图片以中心进行裁剪，裁剪出的图片就是非常适合做引导页的图片了
                Bitmap finallyBitmap = Bitmap.createBitmap(scaledBitmap, 0, offset, scaledBitmap.getWidth(),
                        scaledBitmap.getHeight() - offset * 2);


                if (!finallyBitmap.equals(scaledBitmap)) {//如果返回的不是原图，则对原图进行回收
                    scaledBitmap.recycle();
                    System.gc();
                }


                // 设置图片显示
                view.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), finallyBitmap));
                return true;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);

    }
}
